<?php
session_start();
require('../conn.php');
require('../functions.php');
require('../QueryList.class.php');
require('admin.php');
if ($_POST) {
    $long = guolv(trim($_POST['long']));
    $type_arr = explode('#', guolv($_POST['type']));
    $code_arr = explode('#', guolv($_POST['code']));
    $flag_arr = explode('#', guolv($_POST['flag']));
    $type = $type_arr[0]; 
    $code=$code_arr[0];
    $flag=$flag_arr[0];
    $money = $code_arr[1];
    $html = get_contents($long);
    $html = str_replace('data-src', 'src', $html);
    $html = str_replace('src="http://mmbiz', 'src="'.$image_protect.'http://mmbiz', $html);
    $html = str_replace('Trap<body></body><head></head><html></html>', '', $html);
    $vid = cut($html, 'vid=', '&');
    $caiji = array("title" => array(".rich_media_title:first", "text"), "content" => array("#js_content", "html"),);
    $quyu = '';
    $hj = QueryList::Query($html, $caiji, $quyu);
    $arr = $hj->jsonArr;
    $title = $arr[0]['title'];
    $content = preg_replace("/<(\/?i?frame.*?)>/si", "", $arr[0]['content']);
    if ($vid !== '') {
        $content = "<p><iframe height=300 width=100% src=\"http://v.qq.com/iframe/player.html?vid={$vid}&auto=0\" frameborder=0 allowfullscreen></iframe></p>" . $content;
    }
    $pic = cut($html, 'var msg_cdn_url = "', '"');
    if (url_exists($long) == 1) {
        echo "<script>alert('网址不存在');location.href='weixin.php'</script>";
        exit;
    }
    if (is_numeric($type) == false) {
        echo "<script>alert('分类不存在');location.href='weixin.php'</script>";
        exit;
    }
    $row = $mysql->query("select * from `article` where `title`='{$title}' limit 1");
    if (!$row) {
        $arr = array('top' => -1, 'title' => $title, 'content' => $content, 'pic' => $pic, 'type' => $type,'code' => $code, 'day' => date("Y-m-d", time()),'flag' => 0);
        $value = arr2s($arr);
        $mysql->query("insert into `article` {$value}");
        $id = mysql_insert_id();
        if ($id !== 0) {
            echo "<script>location.href='edit_article.php?id={$id}'</script>";
            exit;
        } else {
            echo '<script>alert(\'发布失败\');location.href=\'weixin.php\'</script>';
            exit;
        }
    } else {
        echo "<script>location.href='edit_article.php?id={$row[0]['id']}'</script>";
        exit;
    }
} ?>

<script>
    function changeType(x){



        var type=document.getElementById("type");
var form2=type.length;//这句解释同上 

var diqul=new Array(form2)//新建一个数组，项数为第一个下拉列表的项数 
for(i=0;i<form2;i++)

//循环第一个下拉列表的项数那么多次 
diqul[i]=new Array();//子循环 
//下面是给每个循环赋值 
// var shi=$("#shi");//方便引用 

var code=document.getElementById("code");

<?php
$name = array ();
$type = array ();
$number = array ();
$code_pp=array();
$row_code = $mysql->query ( "select name,type,number,code_pp from `codedata` " );
foreach ( $row_code as $key ) {
    $name [] = $key ['name'];
    $type [] = $key ['type'];
    $number [] = $key ['number'];
    $code_pp[]=$key['code_pp'];
}
$new_name = json_encode ( $name );
$new_type = json_encode ( $type );
$new_number = json_encode ( $number );
$new_code_pp=json_encode ( $code_pp );
echo "var dataname =" . $new_name;
echo ";";
echo "var datatype =" . $new_type;
echo ";";
echo "var datanumber =" . $new_number;
echo ";";
echo "var datacode_pp =" . $new_code_pp;
?>

for (var i = 0; i < dataname.length; i++)      {
   number=parseInt(datanumber[i])+1;
    // diqul[datatype[i]][datanumber[i]]=new Option('['+$number+']'+ dataname[i]+'(单价：'+datacode_pp[i]+')',dataname[i]); 
   diqul[datatype[i]][datanumber[i]]=new Option('['+number+']'+ dataname[i]+'(单价：'+datacode_pp[i]+')',number + '#' + datacode_pp[i]); 


}

for(m=code.length-1;m>0;m--) 
code.options[m]=null;//将该项设置为空,也就等于清除了 
for(j=0;j<diqul[x].length;j++){//这个循环是填充下拉列表 
code.options[j]=new Option(diqul[x][j].text,diqul[x][j].value); 
//注意上面这据,列表的当前项等于 新项(数组对象的x,j项的文本为文本，) 
} 
code.options[0].selected=true;//设置被选中的初始值 
    }
</script>




<?php include('head.php') ?>
<div class="container-fluid">

    <div class="row-fluid">
        <?php
        include('left.php'); ?>
        <div class="span10">
            <h1 class="page-title">微信文章导入</h1>
            <!--
            <div class="btn-toolbar">
                <a href="#myModal" data-toggle="modal" class="btn">Delete</a>
              <div class="btn-group">
              </div>
            </div>
            -->
            <div class="well">
                <ul class="nav nav-tabs">
                    <li class="active"><a href="#home" data-toggle="tab">发布</a></li>
                    <li><a href="http://weixin.sogou.com" target="_blank">更多微信文章</a></li>
                </ul>
                <div id="myTabContent" class="tab-content">
                    <div class="tab-pane active in" id="home">
                        <form id="tab" action="weixin.php" method="post">
                             <label>分类</label>
                        <select id="type" name="type"  onchange="changeType(options.selectedIndex)"> 
                            <?php
                            $row_type = $mysql->query("select * from `typedata` order by `id` ");
                            foreach ($row_type as $v_type) {
                                echo "<option value=\"{$v_type['type']}#{$v_type['type_pp']}\">[{$v_type['id']}] {$v_type['name']}</option>";
                            } ?>
                        </select>
                        <label>标签</label>
                        <select id="code" name="code" >
                            <?php
                            $row_code = $mysql->query("select * from `codedata` where type=0 ");
                            foreach ($row_code as $v_code) {
                                $number=$v_code['number']+1;
                                echo "<option value=\"{$number}#{$v_code['code_pp']}\">[$number] {$v_code['name']}(单价：{$v_code['code_pp']})</option>";
                            } ?>
                        </select>
                        <label>标志</label>
                        <select id="flag" name="flag" >                         
                              <option value=0>[1]普通</option>
                              <option value=1>[2]轮播</option>
                              <option value=2>[3]推送</option> 
                              <option value=3>[4]推荐</option>             
                        </select>
                            <label>微信公众号网址：</label>
                            <input type="text" value="" style="width:500px" name="long"><br/>
                            <p>例如：<code>http://mp.weixin.qq.com/s?__biz=MjM5OTA2MzE3Mg==&mid=234288765&idx=2&sn=ed9e013786f33d51326125a61b0af8b8&3rd=MzA3MDU4NTYzMw==&scene=6#rd</code>
                            </p>
                            <label></label>
                            <button class="btn btn-primary"><i class="icon-save"></i> 提交</button>
                        </form>
                    </div>
                    <div class="tab-pane fade" id="profile">
                        <form id="tab2">
                            <label>New Password</label>
                            <input type="password" class="input-xlarge">
                            <div>
                                <button class="btn btn-primary">Update</button>
                            </div>
                        </form>
                    </div>
                </div>

            </div>

            <div class="modal small hide fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"
                 aria-hidden="true">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                    <h3 id="myModalLabel">Delete Confirmation</h3>
                </div>
                <div class="modal-body">

                    <p class="error-text"><i class="icon-warning-sign modal-icon"></i>Are you sure you want to delete
                        the user?</p>
                </div>
                <div class="modal-footer">
                    <button class="btn" data-dismiss="modal" aria-hidden="true">Cancel</button>
                    <button class="btn btn-danger" data-dismiss="modal">Delete</button>
                </div>
            </div>
        </div>
    </div>
</div>

<footer>
    <hr>
    <p class="pull-right"><!--power by right--></p>
    <p><!--power by --></p>
</footer>


<!-- Le javascript
================================================== -->
<!-- Placed at the end of the document so the pages load faster -->
<script src="lib/bootstrap/js/bootstrap.js"></script>


</body>
</html>